package com.yfjin.gl.filter;

import android.content.Context;
import android.opengl.GLES20;
import android.util.Log;

import com.yfjin.gl.R;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;


public class ViewFilter {


    protected int mProgramId;

    //每个顶点坐标的个数
    private final static int COORDS_PER_VERTEX = 2;
    //每个顶点颜色的个数
    private final static int COLOR_PER_VERTEX = 3;
    // 浮点类型占用的字节数
    private final static int BYTES_PER_FLOAT = 4;

    //下面两个字符串常量就是GLSL顶点着色器的输入
    private static final String A_POSITION = "a_Position";
    private static final String A_COLOR = "a_Color";


    //STRIDE是一个顶点的字节偏移（顶点坐标xy+颜色rgb）
    private final int STRIDE = (COORDS_PER_VERTEX + COLOR_PER_VERTEX) * BYTES_PER_FLOAT;

    private FloatBuffer mVertexBuffer;


    //顶点数组
    private final float[] TRIANGLE_COORDS = {
            0.5f, 0.5f, 1f, 0f, 0f,
            -0.5f, -0.5f, 0f, 1f, 0f,
            0.5f, -0.5f, 0f, 0f, 1f
    };


    public ViewFilter(Context context) {

        String vertexShader = OpenGLUtils.readRawTextFile(context, R.raw.view_vert);
        String fragShader = OpenGLUtils.readRawTextFile(context, R.raw.view_frag);
        //编译-》连接-》运行
        mProgramId = OpenGLUtils.loadProgram(vertexShader, fragShader);

        initData();
    }


    private void initData() {
        //通过nio ByteBuffer把设置的顶点数据加载到内存
        mVertexBuffer = ByteBuffer
                .allocateDirect(TRIANGLE_COORDS.length * BYTES_PER_FLOAT) //需要多少字节内存
                .order(ByteOrder.nativeOrder())//大小端排序
                .asFloatBuffer()
                .put(TRIANGLE_COORDS);//设置数据

        //--------------------------------------------------------------------------
        GLES20.glUseProgram(mProgramId);
        //--------------------------------------------------------------------------

        mVertexBuffer.position(0);
        int aPosition = GLES20.glGetAttribLocation(mProgramId, A_POSITION);
        GLES20.glVertexAttribPointer(
                aPosition,//aPosition attribute 句柄
                COORDS_PER_VERTEX,//coords 用几个偏移描述一个顶点/在数组中占用的位数
                GLES20.GL_FLOAT,//顶点数据类型
                false,
                STRIDE,//一个顶点需要多少个字节偏移
                mVertexBuffer//分配的buffer  元数据
        );
        //开启顶点着色器的attribute
        GLES20.glEnableVertexAttribArray(aPosition);

        //--------------------------------------------------------------------------
        mVertexBuffer.position(COORDS_PER_VERTEX);
        int aColor = GLES20.glGetAttribLocation(mProgramId, A_COLOR);

        GLES20.glVertexAttribPointer(
                aColor,
                COLOR_PER_VERTEX, //color
                GLES20.GL_FLOAT,
                false,
                STRIDE,
                mVertexBuffer
        );
        //开启顶点着色器的attribute
        GLES20.glEnableVertexAttribArray(aColor);

    }


    public void onDraw() {
        //清屏
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        //绘制
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 3);
//        是的，在onDrawFrame中进行不断的 glDrawArrays来绘制刷新
//        public static native void glDrawArrays(
//        int mode, //点、线、三角形
//        int first,//顶点的第一个数据的index
//        int count//顶点的总数
//      );
    }


    public void release() {
        GLES20.glDeleteProgram(mProgramId);
    }
}


